/**
 * @file vector_ie1.c
 * Copyright (c) 2022 Gaaagaa. All rights reserved.
 * 
 * @author  : Gaaagaa
 * @date    : 2022-10-26
 * @version : 1.0.0.0
 * @brief   : 使用 向量 容器进行数据的 插入/删除 操作。
 */

/**
 * The MIT License (MIT)
 * Copyright (c) Gaaagaa. All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "std_vector.h"
#include "std_mdebug.h"
#include <stdio.h>

////////////////////////////////////////////////////////////////////////////////

/**********************************************************/
/**
 * @brief 打印输出 xstd_vector_t 对象的各个节点元素。
 */
xstd_void_t printf_vector_int32(xvct_this_t xvct_this)
{
    xstd_size_t xst_count = 0;
    xvct_iter_t xvct_iter = std_vector_begin(xvct_this);
    xvct_iter_t xvct_vend = std_vector_end(xvct_this);

    if (std_vector_size(xvct_this) <= 0)
    {
        printf("vector(0) \n");
        return;
    }

    printf("vector(%d) [[[\n", (xstd_int32_t)std_vector_size(xvct_this));

    for (xst_count  = 0;
         xvct_iter != xvct_vend;
         xvct_iter  = std_vector_next(xvct_this, xvct_iter))
    {
        printf("[%3d]=(%3d)",
            (xstd_int32_t)xst_count, std_vector_elem_int32(xvct_this, xvct_iter));

        if (++xst_count < std_vector_size(xvct_this))
        {
            if (0 == (xst_count & 7))
                printf("\n");
            else
                printf(" , ");
        }
        else
        {
            printf("\n]]] vector\n\n");
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

int main(int argc, char * argv[])
{
    xmdb_this_t xmdb_comm = XSTD_NULL;
    xstd_int32_t  xit_value = 0;
    xstd_int32_t  xit_count = 64;
    xvct_this_t xvct_this = XSTD_NULL;

    //======================================

    xmdb_comm = (xmdb_this_t)std_mdbcomm_start();

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    xvct_this = std_vector_create_int32();

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // insert middle

    printf("INERT middle: ");

    for (xit_value = 1; xit_value <= xit_count; ++xit_value)
    {
        std_vector_insert_int32(
            xvct_this,
            std_vector_iter(xvct_this, std_vector_size(xvct_this) / 2),
            xit_value);
    }

    printf_vector_int32(xvct_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // insert begin

    printf("INERT begin: ");

    for (xit_value = 1; xit_value <= 8; ++xit_value)
    {
        std_vector_insert_int32(
            xvct_this,
            std_vector_begin(xvct_this),
            xit_value);
    }

    printf_vector_int32(xvct_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // insert end

    printf("INERT end: ");

    for (xit_value = 8; xit_value >= 1; --xit_value)
    {
        std_vector_insert_int32(
            xvct_this,
            std_vector_end(xvct_this),
            xit_value);
    }

    printf_vector_int32(xvct_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // insert n

    printf("INERT n: ");

    for (xit_value = 1; xit_value <= 4; ++xit_value)
    {
        std_vector_insert_n_int32(
            xvct_this,
            std_vector_iter(xvct_this, std_vector_size(xvct_this) / 2),
            8,
            xit_value + 256);
    }

    printf_vector_int32(xvct_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // erase n

    printf("INERT n: ");

    for (xit_value = 1; xit_value <= 32; ++xit_value)
    {
        std_vector_erase(xvct_this, std_vector_iter(xvct_this, std_vector_size(xvct_this) / 2));
    }

    printf_vector_int32(xvct_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // erase begin

    printf("INERT begin: ");

    for (xit_value = 1; xit_value <= 8; ++xit_value)
    {
        std_vector_erase(xvct_this, std_vector_begin(xvct_this));
    }

    printf_vector_int32(xvct_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // erase rbegin

    printf("INERT rbegin: ");

    for (xit_value = 1; xit_value <= 8; ++xit_value)
    {
        std_vector_erase(xvct_this, std_vector_rbegin(xvct_this));
    }

    printf_vector_int32(xvct_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // erase range

    printf("INERT range: ");

    std_vector_erase_range(
        xvct_this,
        std_vector_iter(xvct_this, std_vector_size(xvct_this) * 1 / 4),
        std_vector_iter(xvct_this, std_vector_size(xvct_this) * 3 / 4));

    printf_vector_int32(xvct_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // shrink

    printf("SHRINK : ");
    std_vector_shrink(xvct_this);
    printf_vector_int32(xvct_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================

    std_vector_destroy(xvct_this);

    printf("%d. mem count : %d\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    std_mdbcomm_stop();

    //======================================

    return 0;
}
